-- 坚果类
local Plant = require("app.mode.Plant")
local AniHelper = require("app.utils.AnimateHelper")

local WallNut = {}
setmetatable(WallNut, Plant)
WallNut.__index = WallNut

WallNut.NORMAL = 0
WallNut.CRACK1 = 1
WallNut.CRACK2 = 2

function WallNut:initView()
    if self:getState() == WallNut.NORMAL then
        self._sprite, self._animate = AniHelper("plants/" , self:getName())
    elseif self:getState() == WallNut.CRACK1 then
        self._sprite, self._animate = AniHelper("plants/" , self:getName() .. "_cracked1")
    elseif self:getState() == WallNut.CRACK2 then
        self._sprite, self._animate = AniHelper("plants/" , self:getName() .. "_cracked2")
    end
end

function WallNut:beAttack(attack)
    self:setBlood(self:getBlood() - attack)
    if self:getBlood() <= 0 then
        self:removeSelf()
        -- 移除僵尸的攻击目标
        for _, z in ipairs(self:getTarget()) do
            z:unAttack()
        end
        -- 从场景中移除
        self._scene:removePlant(self)
    elseif self:getBlood() < self:getMaxBlood() * 0.5 and self:getState() < WallNut.CRACK2 then
        self:setState(WallNut.CRACK2)
        local sprite
        sprite, self._animate = AniHelper("plants/" , self:getName() .. "_cracked2")
        self._sprite:setSpriteFrame(sprite:getSpriteFrame())
        self:runAnimate()
    elseif self:getBlood() < self:getMaxBlood() * 0.8 and self:getState() < WallNut.CRACK1 then
        self:setState(WallNut.CRACK1)
        local sprite
        sprite, self._animate = AniHelper("plants/" , self:getName() .. "_cracked1")
        self._sprite:setSpriteFrame(sprite:getSpriteFrame())
        self:runAnimate()
    end
end

function WallNut:getState()
    return self._data.state
end

function WallNut:setState(val)
    self._data.state = val
end

return WallNut
